package com.wings4.number.utils;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 2/20/14
 * Time: 8:57 PM
 * To change this template use File | Settings | File Templates.
 */
public class CodeGeneratorUtil {

    private static final int NUMBER_OF_DIGIT = 9;

    /**
     * This method is used to generate category code
     * @param total , total number of category already created
     * @param parentCode , parent category
     * @return   18 digit code prefixed with parent code
     */
    public static String generateAccountCategoryCode(long total, String parentCode) {
        String code="";
        for(int i=0;i<NUMBER_OF_DIGIT;i++){
            code+="0";
        }
        String iDString = (total +1) + "";
        int length = iDString.length();
        for (int i = 0; i < (NUMBER_OF_DIGIT - length); i++) {
            iDString = "0" + iDString;
        }
        if(parentCode==null||parentCode.isEmpty()){
            code += iDString;
        } else{
            String prefix = parentCode;
            if(parentCode.length()>NUMBER_OF_DIGIT){
                prefix = parentCode.substring(NUMBER_OF_DIGIT);
            }

            code = prefix + iDString;
        }
        return code;
    }

    /**
     * This method is used to generate account head code
     * @param total , total number of head already created
     * @param parentCode , parent head
     * @param category , parent category
     * @return   27 digit code prefixed with parent code
     */
    public static String generateAccountHeadCode(long total, String parentCode,String category) {
        String code="";
        for(int i=0;i<NUMBER_OF_DIGIT;i++){
            code+="0";
        }
        String iDString = (total +1) + "";
        int length = iDString.length();
        for (int i = 0; i < (NUMBER_OF_DIGIT - length); i++) {
            iDString = "0" + iDString;
        }

        return generateAccountHeadCode(iDString,parentCode,category);
    }

    /**
     * This method is used to generate account head code
     * @param fixedCode , total number of head already created
     * @param parentCode , parent head
     * @param category , parent category
     * @return   27 digit code prefixed with parent code
     */
    public static String generateAccountHeadCode(String fixedCode, String parentCode,String category) {
        String code="";
        for(int i=0;i<NUMBER_OF_DIGIT;i++){
            code+="0";
        }
        String iDString = fixedCode;
        int length = iDString.length();
        for (int i = 0; i < (NUMBER_OF_DIGIT - length); i++) {
            iDString = "0" + iDString;
        }
        String prefix=code;
        if(category!=null||!category.isEmpty()){
            prefix = category;
            if(category.length()>NUMBER_OF_DIGIT){
                prefix = category.substring(NUMBER_OF_DIGIT);
            }

        }

        if(parentCode==null||parentCode.isEmpty()){
            prefix+=code;
        } else{
            String parentPrefix=parentCode;
            if(parentCode.length()>NUMBER_OF_DIGIT*2){
                parentPrefix = parentCode.substring(NUMBER_OF_DIGIT*2);
            } else if(parentCode.length()>NUMBER_OF_DIGIT){
                parentPrefix = parentCode.substring(NUMBER_OF_DIGIT);
            }

            prefix += parentPrefix;
        }
        return prefix+iDString;
    }


    /**
     * This method is used to get the short cut value of the code to make code readable
     * @param code , parent category
     * @return   10 digit code prefix with ...
     */
    public static String getDisplayableGenerateAccountCategoryCode(String code) {
        String parentCode=code.substring(0,NUMBER_OF_DIGIT);
        String childCode=code.substring(NUMBER_OF_DIGIT);
        String resultedCode=".."+parentCode.substring(parentCode.length()-3);
        resultedCode+=".."+childCode.substring(childCode.length()-3);
        return resultedCode;
    }

    public static void main(String[] args) {
        int category=6;
        String parentCode="000000000000000009";
        String code=generateAccountCategoryCode(category,parentCode);
        if( code.equalsIgnoreCase("000000009000000007")){
            System.out.println("code  " + code+" generated correctly");
        } else{
            System.out.println("code  " + code+" did not generate correctly");
        }

        category=7;
        parentCode="";
        code=generateAccountCategoryCode(category,parentCode);
        if( code.equalsIgnoreCase("000000000000000008")){
            System.out.println("code  " + code+" generated correctly");
        } else{
            System.out.println("code  " + code+" did not generate correctly");
        }

        String categoryCode="000000009000000010";
        code=getDisplayableGenerateAccountCategoryCode(categoryCode);
        if( code.equalsIgnoreCase("..009..010")){
            System.out.println("code  " + code+" generated correctly");
        } else{
            System.out.println("code  " + code+" did not generate correctly");
        }


        // account head test
        categoryCode="000000009000000010";
        parentCode="000000013000000001000000009";
        code=generateAccountHeadCode(9,parentCode,categoryCode);
        if( code.equalsIgnoreCase("000000010000000009000000010")){
            System.out.println("code  " + code+" generated correctly");
        } else{
            System.out.println("code  " + code+" did not generate correctly");
        }

        // account head test
        categoryCode="000000009000000010";
        parentCode="000000009000000010000000009";
        code=generateAccountHeadCode("000000012",parentCode,categoryCode);
        if( code.equalsIgnoreCase("000000010000000009000000012")){
            System.out.println("code  " + code+" generated correctly");
        } else{
            System.out.println("code  " + code+" did not generate correctly");
        }
    }
}
